package ibmetp.mis.dao;

import ibmetp.mis.vo.Page;
import ibmetp.mis.vo.CodeList;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CodeListDao extends BaseDao{
	private Connection conn;

	/**
	 * 通过代码类型ID获取代码类型信息
	 * @code id
	 * @return
	 */
	public CodeList getCodeListById(Long id){
		String sql="select r.* from BAS_T_CODE_TYPE r where r.CODE_TYPE_ID=?";
		System.out.println(sql);
		conn=DBUtil.getConnection();
		PreparedStatement ps;
		CodeList codelist=null;
		CodeListDao ct = new CodeListDao();
		try{
			ps=conn.prepareStatement(sql);
			ps.setLong(1, id);
			ResultSet rs=ps.executeQuery();
			while(rs!=null && rs.next())
			{
				codelist=new CodeList();
				codelist.setCodeListId(rs.getLong("ROLE_ID"));
				codelist.setCodeListId(rs.getLong("ROLE_TYPE"));
				codelist.setCodeValue(rs.getString("CODE_VALUE"));
				codelist.setDescript(rs.getString("DESCRIPT"));
				codelist.setSort(rs.getLong("SORT"));
			}
			ps.close();
			conn.close();			
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		return codelist;
	}
	
	/**
	 * 通过调用对象codelistVO添加数据
	 * @code codelistVO
	 * @return
	 */
	public String addListCode(CodeList codelistVO) 
	{
		String addSQL="insert into CODE_LIST (CODELIST_ID,CODE_LIST_NAME,DESCRIPT,DESCRIPT,SORT)" +
		" values(?,?,?,?,?)";
		
		conn=DBUtil.getConnection();
		try{
			PreparedStatement ps= conn.prepareStatement(addSQL);
			ps.setLong(1, codelistVO.getCodeListId());
			ps.setLong(2, codelistVO.getCodeType());
			ps.setString(3, codelistVO.getCodeValue());
			ps.setString(4, codelistVO.getDescript());
			ps.setLong(5, codelistVO.getSort());
			ResultSet rs = ps.executeQuery();
//		return ps.executeUpdate();
		}catch(SQLException e){
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 通过userIds删除数据
	 * @param CodeListId
	 * @return
	 * @throws SQLException
	 */
	
	public void deleteCodeListById(String CodeListIds)
	{
		String delSQL="delete CODELIST where USER_ID in ("+CodeListIds+")";
//		String delSQL="delete CODELIST where USER_ID in ("+array2String(CodeListIds)+")";
		conn=DBUtil.getConnection();
		Statement ps;
		try {
			ps = conn.createStatement();
			String ids=CodeListIds;
//			String ids=array2String(CodeListIds);
			int i= ps.executeUpdate(delSQL);
			ps.close();
			conn.close();
		}catch(SQLException e){
			e.printStackTrace();
		}			
	}
	
	/**
	 * 通过调用对象codelistVO更新数据
	 * @param codelistVO
	 * @throws SQLException
	 */
	
	public void updateCodeList(CodeList codelistVO)
	{
		String addSQL="update CODELIST set (CODELIST_ID=?,CODE_LIST=?,VALUE=?,CATEGORY=?,CREATE_BY=?,CREATE_DATE=?,UPDATE_BY=?,UPDATE_DATE=?)" +
						" where CODELIST_ID=?";
		conn=DBUtil.getConnection();
		try{
			PreparedStatement ps= conn.prepareStatement(addSQL);
			ps.setLong(1, codelistVO.getCodeListId());
			ps.setLong(2, codelistVO.getCodeType());

			ResultSet rs = ps.executeQuery();
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	
	 /**
	  * 数组转换为字符串
	  * @param strs
	  * @return
	  */
	  
	public static String array2String(String[] strs)
	{
		if(strs==null)
		{
			return "";
		}
		String result="";
		for (String string : strs) {
			
			result+=string+",";
		}
		return result.substring(0,result.length()-1);
	}
	
	
	public Page getcodelistPage(int pageNo,int pageSize,Object codelist)
	{		
		return this.find(codelist, pageNo, pageSize, "BAS_T_CODELIST");		
	}
	
	@Override
	/**
	 * 将查询结果转换成codelist类集，并保存进List集合中
	 */
	public List ResultSet2List(ResultSet rs) {
		// TODO Auto-generated method stub
		List<CodeList> resultList=new ArrayList<CodeList>();
		try {
			while(rs!=null&&rs.next())
			{
				CodeList codelist=new CodeList();
				codelist.setCodeListId(rs.getLong("CODE_LIST_ID"));
				codelist.setCodeType(rs.getLong("CODE_TYPE"));
				codelist.setDescript("DESCRIPT");
				resultList.add(codelist);
			}
		}catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return resultList;
	}
	
	@Override
	/**
	 * 添加查询条件
	 */
	public Map<Integer, Object[]> getCondition(Object o) {
		// TODO Auto-generated method stub
		Map<Integer,Object[]> codelistMap=new HashMap<Integer,Object[]>();
		Integer count=0;
		CodeList codelist=(CodeList)o;
		if(codelist.getCodeListId()!=null&&codelist.getCodeListId()>0)
		{
			count++;
			codelistMap.put(count, new Object[]{" and CODE_LIST_ID LIKE ?","%"+codelist.getCodeListId()+"%"});
		}
		if(codelist.getCodeType()!=null&& codelist.getCodeType().SIZE>0)
		{
			count++;
			codelistMap.put(count, new Object[]{"and CODE_LIST LIKE ?","%"+codelist.toString()+"%"});
		}
		return codelistMap;
	}
}